package com.hq.ims.data.event.listener;

import com.hq.ims.data.entity.BusInfoFeedback;
import com.hq.ims.data.entity.BusiInfo;
import com.hq.ims.data.entityenum.EnumFeedbackPhase;
import com.hq.ims.data.entityenum.EnumForewarningInfoClassify;
import com.hq.ims.data.entityenum.EnumPlatformTodoType;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.event.BusinfoFeedbackEvent;
import com.hq.ims.data.service.*;
import com.hq.ims.data.vo.BusInfoForewarningMapstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 用户跟踪反馈事件
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class BusinfoFeedbackListener {

    @Resource
    private BasePlatformTodoStatisticsService basePlatformTodoStatisticsService;
    @Resource
    private BaseUserTodoStatisticsService baseUserTodoStatisticsService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;
    @Resource
    private BaseBusInfoForewarningService baseBusInfoForewarningService;

    @Async
    @TransactionalEventListener
    public void onApplicationEvent(BusinfoFeedbackEvent businfoEvent) {
        String eventType = businfoEvent.getEventType();

        switch (eventType) {
            case BusinfoFeedbackEvent.ADD_EVENT:
                this.addEventHandler(businfoEvent);
                break;
            default:
                break;
        }
    }

    /**
     * 添加事件处理
     */
    public void addEventHandler(BusinfoFeedbackEvent businfoFeedbackEvent) {
        //平台端反馈通知+1
        basePlatformTodoStatisticsService.updateCountByPlatformTodoType(
                EnumPlatformTodoType.BusinfoFeedbackNotifyCount,
                1,
                businfoFeedbackEvent.getBusInfoFeedback().getBusInfoFeedbackId()
        );
        //用户端给当前经营信息父数据的创建人反馈通知+1
        BusiInfo busiInfo = baseBusiInfoService.getById(businfoFeedbackEvent.getBusInfoFeedback().getBusiInfoId());
        if (busiInfo == null) {
            return;
        }
        BusiInfo parentBusiInfo = baseBusiInfoService.getById(busiInfo.getParentId());
        if (parentBusiInfo != null) {
            baseUserTodoStatisticsService.updateCountByUserTodoType(
                    EnumUserTodoType.BusinfoFeedbackNotifyCount,
                    1,
                    busiInfo.getBusiInfoId(),
                    parentBusiInfo.getPublisherId(),
                    parentBusiInfo.getOrgId()
            );
        }

        BusInfoFeedback busInfoFeedback = businfoFeedbackEvent.getBusInfoFeedback();
        // 判断如果是反馈阶段是结束,则直接移除预警
        if (EnumFeedbackPhase.End.equals(busInfoFeedback.getFeedbackPhase())) {
            baseBusInfoForewarningService.removeForewarning(busInfoFeedback.getBusInfoTrackId());
        } else {
            // 添加未反馈预警
            BusInfoForewarningMapstruct.AddForewarningVo addForewarningVo = new BusInfoForewarningMapstruct.AddForewarningVo();
            addForewarningVo.setForewarningInfoClassify(EnumForewarningInfoClassify.NotFeedbackForewarning);
            addForewarningVo.setBusInfoFeedback(busInfoFeedback);
            baseBusInfoForewarningService.addForewarning(addForewarningVo);
        }
    }

}
